package com.zdzx.controller.admin.base;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdzx.base.common.util.RedisUtil;
import com.zdzx.base.common.util.UserInfoUtil;
import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.CertificateMapper;
import com.zdzx.mapper.CertificateUserMapper;
import com.zdzx.mapper.TransactionRecordMapper;
import com.zdzx.mapper.UserMapper;
import com.zdzx.model.service.base.result.IndexCertificate;
import com.zdzx.model.service.base.result.IndexPay;
import com.zdzx.model.service.base.result.IndexUse;
import com.zdzx.model.service.base.result.IndexUserCount;
import com.zdzx.model.service.base.result.UserInfo;
import com.zdzx.model.service.base.vo.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "首页")
@RestController
@RequestMapping("/admin")
public class IndexCountCountroller {

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private TransactionRecordMapper transactionRecordMapper;
	@Autowired
	private CertificateMapper certificateMapper;
	@Autowired
	private CertificateUserMapper certificateUserMapper;
	@Autowired
	private RedisUtil redisUtil;
	@ApiOperation(value = "用户", notes = "")
	@GetMapping("/index/user")
	public Result<IndexUserCount> countUser(HttpServletRequest request){
		UserInfo loginInfo = UserInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		QueryWrapper<User> queryWrapper=new QueryWrapper<User>();
		queryWrapper.eq("tenant_id", loginInfo.getTenantId());
		Integer userCount = userMapper.selectCount(queryWrapper);
		Integer studyCount = userMapper.selectStudyCount(loginInfo.getTenantId());
		
		IndexUserCount c=new IndexUserCount();
		c.setUserCount(userCount==null?0:userCount);
		c.setStudyCount(studyCount==null?0:studyCount);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, c);
	}
	
	@ApiOperation(value = "证书", notes = "")
	@GetMapping("/index/certificate")
	public Result<IndexCertificate> countCertificate(HttpServletRequest request){
		//UserInfo loginInfo = UserInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		
		Integer certificateCount = certificateMapper.selectCount(null);
		Integer certificateUserCount = certificateUserMapper.selectCount(null);
		
		IndexCertificate c=new IndexCertificate();
		c.setCertificateCount(certificateCount==null?0:certificateCount);
		c.setCertificateUserCount(certificateUserCount==null?0:certificateUserCount);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, c);
	}
	
	
	@ApiOperation(value = "交易", notes = "")
	@GetMapping("/index/pay")
	public Result<IndexPay> countTransactionRecord(){
		IndexPay indexPay = transactionRecordMapper.sumMoney();
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, indexPay==null?new IndexPay():indexPay);
	}
	
	@ApiOperation(value = "存储使用", notes = "")
	@GetMapping("/index/use")
	public Result<IndexUse> use(){
		File file = new File("/home/jar/data");  
	    long totalSpace = file.getTotalSpace(); 
	    long usableSpace = file.getUsableSpace();  
	    long freeSpace = file.getFreeSpace(); 

	    IndexUse indexUse=new IndexUse();
	    indexUse.setTotalSpace(totalSpace /1024 /1024 + " mb");  
	    indexUse.setUsableSpace(usableSpace /1024 /1024 + " mb");
	    indexUse.setFreeSpace(freeSpace /1024 /1024 + " mb");
	   return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null, indexUse);
	}
	
	
	
	
}
